package jch.learn.java.advanced.chapter2_InputAndOutput.objectStream;

import java.io.*;

public class ObjectStreamTest {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Employee one = new Employee("one", 100000, 1999, 10, 10);
        Manager bossOne = new Manager("bossOne", 200000, 2000, 1, 1);
        bossOne.setSecretary(one);
        Manager bossTwo = new Manager("bossTwo", 1, 1949, 10, 1);
        bossTwo.setSecretary(one);

        Employee[] staff = new Employee[3];
        staff[0] = one;
        staff[1] = bossOne;
        staff[2] = bossTwo;

        String path = ObjectStreamTest.class.getResource("/chapter2/employee.dat").getPath();
        // 对象数组写入到文件中
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path))) {
            out.writeObject(staff);
        }

        // 从文件中读取对象数组
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(path))) {

            Employee[] newStaff = (Employee[]) in.readObject();

            newStaff[1].raiseSalary(10);

            for (Employee e : newStaff) {
                System.out.println(e);
            }
        }

    }

}
